package com.demo.component

import android.app.Activity
import android.util.Log
import com.base.tool.log.LogPrint
import com.demo.Tags
import java.util.concurrent.atomic.AtomicReference


/**
这个线程一旦执行run方法，马上就把自己挂起来了，等待notify()或者notifyAll()，然而没有其它线程来唤醒它，因此它会一直活着，所持有的资源也不会释放
 **/
class LeakingThread2 : Thread() {
    val TAG = Tags.LeakingThread

    //
//  val leakedViews = mutableListOf<View>()
//
//  init {
//    name = "Leaking thread"
//    start()
//    LogPrint.d(TAG, "Start Thread")
//  }
    private val ref = AtomicReference<Activity>(null)

    fun setRef(obj: Activity) {
        ref.set(obj)
    }

    override fun run() {
        val activity = ref.get()
        Log.d(TAG, "activity=$activity");
//        ref.set(null)
//    LogPrint.d(TAG, "List:$leakedViews")
//
    synchronized(obj) {
      LogPrint.d(TAG, "I get the lock, and do something")
      obj.wait()//挂起线程，直到线程得到notify()或者notifyAll()消息

      LogPrint.d(TAG, "continue")

    }
//
        LogPrint.d(TAG, "I'm finish!")

    }

//  companion object {
    private val obj = Object()
//    val thread = LeakingThread2()
//  }
}